package com.houpu.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.houpu.pojo.Emp;
import com.houpu.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.*;

/**
 * @author liu
 * @create 2019-11-09-14:37
 */
@RestController
public class EmpController {
    @Autowired
    private EmpService empService;

    /**
     * 查询员工信息
     *
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping(value = "selectEmp",method = RequestMethod.GET)
    public Map<String, Object> empSelect(int page,int limit) {
        PageHelper.startPage(page, limit);
        List<Emp> listup = empService.queryAll();
        PageInfo<Emp> pageInfo = new PageInfo<>(listup,limit);
        HashMap<String, Object> result = new HashMap<>();
        List<Map<String,Object>> list1 = new ArrayList<>();
        List<Emp> list = pageInfo.getList();
        list.forEach(e->{
            Map<String,Object> map = new HashMap<>();
            map.put("e_id",e.getE_id());
            map.put("p_id",e.getP_id());
            map.put("p_name",e.getDepartment().get(0).getP_name());
            map.put("e_truename",e.getE_truename());
            map.put("e_loginname",e.getE_loginname());
            map.put("e_psw",e.getE_psw());
            map.put("e_sex",e.getE_sex());
            map.put("e_img",e.getE_img());
            map.put("e_flag",e.getE_flag());
            map.put("e_admin",e.getE_admin());
            list1.add(map);
        });
        System.out.println(list1);
        result.put("code", 0);
        result.put("data",list1);
        result.put("count", pageInfo.getTotal());
        return result;
    }

    /**
     * 查询离职员工
     *
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping(value = "selectE_is",method = RequestMethod.GET)
    public Map<String, Object> empSelectE_is(int page,int limit) {
        PageHelper.startPage(page, limit);
        List<Emp> listup = empService.queryEmpE_isor0();
        PageInfo<Emp> pageInfo = new PageInfo<>(listup,limit);
        HashMap<String, Object> result = new HashMap<>();
        List<Map<String,Object>> list1 = new ArrayList<>();
        List<Emp> list = pageInfo.getList();
        list.forEach(e->{
            Map<String,Object> map = new HashMap<>();
            map.put("e_id",e.getE_id());
            map.put("p_id",e.getP_id());
            map.put("p_name",e.getDepartment().get(0).getP_name());
            map.put("e_truename",e.getE_truename());
            map.put("e_loginname",e.getE_loginname());
            map.put("e_psw",e.getE_psw());
            map.put("e_sex",e.getE_sex());
            map.put("e_img",e.getE_img());
            map.put("e_flag",e.getE_flag());
            map.put("e_admin",e.getE_admin());
            list1.add(map);
        });
        System.out.println(list1);
        result.put("code", 0);
        result.put("data",list1);
        result.put("count", pageInfo.getTotal());
        return result;
    }

    /**
     * 查询部门名称：注入下拉框
     *
     * @return
     */
    @RequestMapping(value = "selectP_name",method = RequestMethod.POST)
    public List empP_name() {
        List<String> listP_name = empService.queryP_name();
        PageInfo<String> pageInfo = new PageInfo<>(listP_name);
        HashMap<String, Object> result = new HashMap<>();
        result.put("code", 0);
        result.put("data", pageInfo.getList());
        result.put("count", pageInfo.getTotal());
        return pageInfo.getList();
    }

    /**
     *
     * 添加Emp员工
     * @param emp
     * @return
     */
    @RequestMapping(value = "addEmp", method = RequestMethod.POST)
    public Map<String, Object> add(@RequestBody Emp emp) {
        System.out.println(emp);

        //addEmp
        empService.insert(emp);

        HashMap<String, Object> result = new HashMap<>();

        result.put("status", "0");
        result.put("msg", "执行成功");
        return result;
    }

    /**
     * 修改员工信息
     *
     * @param emp
     * @return
     */
    @RequestMapping(value = "updateEmp", method = RequestMethod.POST)
    public Map<String, Object> update(@RequestBody Emp emp) {
        System.out.println(emp);

        //updateEmp
        empService.update(emp);
        HashMap<String, Object> result = new HashMap<>();
        result.put("status", "2");
        result.put("msg", "执行成功");
        return result;
    }


    /**
     * 离职员工
     *
     * @param emp
     * @return
     */
    @RequestMapping(value = "updateE_is", method = RequestMethod.POST)
    public Map<String, Object> delete(@RequestBody Emp emp) {
        System.out.println(emp);

        //insert
        empService.delete(emp);

        HashMap<String, Object> result = new HashMap<>();
        result.put("status", "1");
        result.put("msg", "执行成功");
        return result;
    }

    /**
     * 恢复员工
     *
     * @param emp
     * @return
     */
    @RequestMapping(value = "updateE_isor1", method = RequestMethod.POST)
    public Map<String, Object> returndelete(@RequestBody Emp emp) {
        System.out.println(emp);

        //insert
        empService.updateE_isor1(emp);

        HashMap<String, Object> result = new HashMap<>();
        result.put("status", "1");
        result.put("msg", "执行成功");
        return result;
    }

    //添加Emp员工
    @RequestMapping(value = "upload" ,method = RequestMethod.POST)
    public String fileUpload(MultipartFile file, Emp emp) throws  IOException {
        /**
         * 上传图片
         */
        //图片上传成功后，将图片的地址写到数据库
        //保存图片的路径（这是存在我项目中的images下了，你们可以设置路径）
        String filePath = "F:\\";
        //获取原始图片的拓展名
        String originalFilename = file.getOriginalFilename();
        //新的文件名字
        String newFileName = UUID.randomUUID() + originalFilename;
        //封装上传文件位置的全路径
        File targetFile = new File(filePath, newFileName);
        //把本地文件上传到封装上传文件位置的全路径
        file.transferTo(targetFile);
        emp.setE_img(newFileName);
//        /**
//         * 保存商品
//         */
        empService.insert(emp);
        return "redirect:/product/list";
    }

}
